package org.dxy.controller;

import org.dxy.entity.Order;
import org.dxy.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

// 订单控制器
@Controller
public class OrderController {
    @Autowired
    private OrderService orderService;

    @PostMapping("/createOrder")
    public String createOrder(@RequestParam Long userId, @RequestParam Long tripId, @RequestParam String seatType, @RequestParam String seatNumber, @RequestParam BigDecimal totalPrice, Model model) {
        Order order = orderService.createOrder(userId, tripId, seatType, seatNumber, totalPrice);
        if (order!= null) {
            model.addAttribute("order", order);
            return "order_confirmation";
        } else {
            model.addAttribute("error", "订单创建失败");
            return "trip_detail";
        }
    }

    @GetMapping("/payOrder/{orderId}")
    public String payOrder(@PathVariable Long orderId, Model model) {
        Order paidOrder = orderService.payOrder(orderId);
        if (paidOrder!= null) {
            model.addAttribute("message", "支付成功");
            return "order_success";
        } else {
            model.addAttribute("error", "支付失败");
            return "order_confirmation";
        }
    }

    @GetMapping("/userOrders")
    public String getUserOrders(@SessionAttribute("userId") Long userId, Model model) {
        List<Order> orders = orderService.getUserOrders(userId);
        model.addAttribute("orders", orders);
        return "user_orders";
    }

    @GetMapping("/cancelOrder/{orderId}")
    public String cancelOrder(@PathVariable Long orderId, Model model) {
        orderService.cancelOrder(orderId);
        model.addAttribute("message", "订单已取消");
        return "redirect:/userOrders";
    }

    // 管理员订单管理相关方法，如查看所有订单、确认订单等
}
